package Thread.ThreadSynchronization;

/*
 * Author: JacksonCoder
 * FileName: SequentialExecutionWithThreadPool
 * Version: 1.0.0
 * Date: 2025/1/23
 */

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SequentialExecutionWithThreadPool {

    public static void main(String[] args) {

        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        // 提交任务 并且控制顺序
        Future<?> future1 = executorService.submit(() -> {
            System.out.println("T1 is running");
        });

        Future<?> future2 = executorService.submit(() -> {
            try {
                future1.get(); // 等待 t1 执行完毕
                System.out.println("T2 is running");

            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        Future<?> future3 = executorService.submit(() -> {
            try {
                future2.get();
                System.out.println("T3 is running");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        // 关闭线程池
        executorService.shutdown();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Threads hava finished execution");
    }

}
